<template>
  <div class="divBox relative">
    <el-card class="box-card" style="min-height: 80vh">
      <div slot="header" class="clearfix" style="margin-bottom: -20px ;">
        <div class="container">
          <el-form :inline="true" :model="queryForm" @submit.native.prevent>
            <!--
                        <el-date-picker v-model="queryForm.timeVal" value-format="yyyy-MM-dd" format="yyyy-MM-dd" size="small" type="daterange" placement="bottom-end" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 250px;" @change="onchangeTime" />
             -->
            <el-form-item>
              <el-select v-model="queryForm.appid" placeholder="请选择app" class="width100" filterable clearable>
                <el-option
                  v-for="item in applist"
                  :key="item.appid"
                  :value="item.appid"
                  :label="item.appname">
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-select v-model="queryForm.orgid" placeholder="请选择机构" class="width100" filterable clearable>
                <el-option
                  v-for="item in orglist"
                  :key="item.id"
                  :value="item.id"
                  :label="item.orgname">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.id" placeholder="请输入设备id" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.userid" placeholder="请输入用户id" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.username" placeholder="请输入用户名称" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <br/>
            <el-form-item>
              <el-input v-model="queryForm.phonebrand" placeholder="请输入手机品牌" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.phonemodel" placeholder="请输入手机型号" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.phonesystem" placeholder="请输入手机系统" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.ip" placeholder="请输入ip" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>

            <el-form-item>
            <el-select v-model="queryForm.lockstatus" placeholder="请选择状态" class="width100" filterable clearable>
              <el-option
                v-for="item in selOptions.status"
                :key="item.value"
                :value="item.value"
                :label="item.label">
              </el-option>
            </el-select>
            </el-form-item>
           <br/>

            <el-form-item>
              <el-input v-model="queryForm.oaid" placeholder="请输入oaid" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.androidid" placeholder="请输入androidid" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.imei" placeholder="请输入imei" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.imsi" placeholder="请输入imsi" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-date-picker v-model="queryForm.timeVal" value-format="yyyy-MM-dd" format="yyyy-MM-dd" size="small"
                              type="daterange" placement="bottom-end" start-placeholder="开始日期"
                              end-placeholder="结束日期" style="width: 250px;" @change="onchangeTime"/>
            </el-form-item>
            <br/>
            <el-form-item label="开发者/无障碍/模拟器/虚拟机: ">
              <el-input v-model="queryForm.devstatus"  style="width: 45px" @keyup.enter.native="search()"/>
              <el-input v-model="queryForm.obstaclestatus"  style="width: 45px" @keyup.enter.native="search()"/>
              <el-input v-model="queryForm.simulatorstatus"  style="width: 45px" @keyup.enter.native="search()"/>
              <el-input v-model="queryForm.virtualstatus"  style="width: 45px" @keyup.enter.native="search()"/>
            </el-form-item>
            <el-form-item label="root/微信/SIM/VPN: ">
              <el-input v-model="queryForm.rootstatus"  style="width: 45px" @keyup.enter.native="search()"/>
              <el-input v-model="queryForm.installwechatstatus"  style="width: 45px" @keyup.enter.native="search()"/>
              <el-input v-model="queryForm.havesimcardstatus"  style="width: 45px" @keyup.enter.native="search()"/>
              <el-input v-model="queryForm.vpnopenstatus"  style="width: 45px" @keyup.enter.native="search()"/>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.coin_begin" placeholder="请输入金币数量" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.todaycoin_begin" placeholder="请输入今日金币" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <br/>
            <el-form-item>
              <el-button @click="search()">查询</el-button>
              <el-button v-if="checkPermi(['device:index:save'])" type="primary" @click="addOrUpdateHandle()">新增
              </el-button>
              <el-button v-if="checkPermi(['device:index:delete'])" type="danger" @click="deleteHandle()"
                         :disabled="dataListSelections.length <= 0">删除
              </el-button>
              <el-button v-if="checkPermi(['device:index:delete'])" type="danger" @click="lockHandle()"
                         :disabled="dataListSelections.length <= 0">封禁
              </el-button>
              <el-button v-if="checkPermi(['loginuser:index:delete'])"  type="danger" @click="resertCoin()"
                         :disabled="dataListSelections.length <= 0">扣量</el-button>

            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-table  :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle"
                style="width: 100%;" size="mini" :highlight-current-row="true"
                :header-cell-style=" {fontWeight:'bold'}">
        <el-table-column type="selection" header-align="center" align="center" width="50"/>
        <el-table-column prop="id" label="设备id">
        </el-table-column>
        <el-table-column prop="userid" label="用户信息"  width="150px">
          <template slot-scope="scope">
            {{ scope.row.userid }} - {{ scope.row.username }}
          </template>
        </el-table-column>
        <el-table-column prop="coin" label="金币总数量">
        </el-table-column>
        <el-table-column prop="todaycoin" label="今日金币">
        </el-table-column>
        <el-table-column prop="phonebrand" label="手机品牌">
        </el-table-column>
        <el-table-column prop="phonemodel" label="手机型号" width="100px">
        </el-table-column>
        <el-table-column prop="phonesystem" label="手机系统" width="80px">
        </el-table-column>
        <el-table-column prop="ip" label="ip" width="190px">
          <template slot-scope="scope">

            [ {{ scope.row.ip }} ]<br/>
            [ {{ scope.row.ipaddress }} ]

          </template>
        </el-table-column>
        <el-table-column label="oaid/androidid/imei/imsi" width="350px">
          <template slot-scope="scope">

            [ {{ scope.row.oaid }} ]<br/>
            [ {{ scope.row.androidid }} ]<br/>
            [ {{ scope.row.imei }} ]<br/>
            [ {{ scope.row.imsi }} ]

          </template>
        </el-table-column>
        <el-table-column prop="devstatus" label="开发者/无障碍/模拟器/虚拟机  root/微信/SIM/VPN" width="180px">
          <template slot-scope="scope">
            {{ scope.row.devstatus }} - {{ scope.row.obstaclestatus }} - {{ scope.row.simulatorstatus }} -
            {{ scope.row.virtualstatus }}<br/>
            {{ scope.row.rootstatus }} - {{ scope.row.installwechatstatus }} - {{ scope.row.havesimcardstatus }} -
            {{ scope.row.vpnopenstatus }}
          </template>
        </el-table-column>


        <!--        <el-table-column prop="status" label="状态" >-->
        <!--        </el-table-column>-->
        <el-table-column prop="appname" label="app" width="120" :show-overflow-tooltip="true"/>
        <el-table-column prop="orgname" label="机构" width="120" :show-overflow-tooltip="true"/>
        <el-table-column prop="lockstatus" label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.lockstatus==0"
              :active-value="true"
              :inactive-value="false"
              active-text="正常"
              inactive-text="冻结"
              @change="change(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="createtime" label="创建时间" width="150px">
        </el-table-column>

        <!--
<template slot-scope="scope" > <el-tag type="success" v-if="scope.row.type===0">免费</el-tag><el-tag type="warn" v-else>付费</el-tag></template>
<template slot-scope="scope" ><el-image style="width: 80px !important;height: 100% !important"  fit="scaleDown" :src="scope.row.icon" :preview-src-list="[scope.row.icon]"/></template>
-->
        <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作" v-if="checkPermi(['device:index:update'])&&checkPermi(['device:index:delete'])">
          <template slot-scope="scope">
            <el-button type="text" v-if="checkPermi(['device:index:update'])" size="small"
                       @click="addOrUpdateHandle(scope.row.id)">修改
            </el-button>
            <el-button type="text" v-if="checkPermi(['device:index:delete'])" size="small"
                       @click="deleteHandle(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="tableForm.pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="tableForm.pageSize"
        :total="tableForm.total"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </el-card>
    <!-- 表单弹窗, 新增数据和修改数据 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './device-add-and-update'
import api from '@/api/api'
import { checkPermi } from '@/utils/permission'
import utils from '@/api/utils'

export default {
  components: {
    AddOrUpdate
  },
  data() {
    return {
      selOptions: {},
      dataList: [],
      applist:[],
      orglist:[],
      queryForm: {
        id: '',
        userid: '',
        username: '',
        phonebrand: '',
        phonemodel: '',
        phonesystem: '',
        ip: '',
        devstatus: '',
        obstaclestatus: '',
        simulatorstatus: '',
        virtualstatus: '',
        vpnopenstatus: '',
        rootstatus: '',
        havesimcardstatus: '',
        installwechatstatus: '',
        oaid: '',
        androidid: '',
        imei: '',
        imsi: '',
        coin: '',
        type: '',
        lockstatus: '',
        lockendtime: '',
        status: '',
        appid: '',
        orgid: '',
        createtime: '',
        updatetime: '',
        createtime_begin: '',
        createtime_end: '',
        todaycoin_begin: '',
        coin_begin: '',
        timeVal: []
      },
      tableForm: {
        pageIndex: 1,
        pageSize: 10,
        total: 0
      },
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  mounted() {
    this.selOptions = this.$constants.selOptions
    this.search()
    utils.getAppList().then(res => {this.applist=res.datalist});
    utils.getOrgList().then(res => {this.orglist=res.datalist});
  },
  methods: {
    checkPermi,
    // 每页数
    sizeChangeHandle(val) {
      this.tableForm.pageSize = val
      this.tableForm.pageIndex = 1
      this.search()
    },
    // 当前页
    currentChangeHandle(val) {
      this.tableForm.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    change(row) {
      let postData = { id: row.id }
      postData.lockstatus = row.lockstatus == 1 ? 0 : 1
      postData.oaids = row.oaid
      api.post('rest/vabc/device/batchLock', postData).then(res => {
        this.$message.success('操作成功')
        row.lockstatus = postData.lockstatus
        // this.getDataList()
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`您确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let postData = { ids: ids.join(',') }
        api.post('rest/vabc/device/delete', postData).then(res => {
          this.$message.success('删除成功')
          this.getDataList()
        })
      })
    },

    lockHandle(oaid) {
      this.$prompt("请输入封禁原因", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputType: 'text',
        inputPattern: /^.+$/,
        inputErrorMessage: '请输入内容'
      }).then(({ value }) => {
        this.$confirm(`您确定对当前用户封禁吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var oaids = oaid ? [oaid] : this.dataListSelections.map(item => {
            return item.oaid
          })
          let postData = { oaids: oaids.join(',') }
          postData.memo = value
          postData.lockstatus = 1
          api.post('rest/vabc/device/batchLock', postData).then(res => {
            this.$message.success('封禁成功')
            this.getDataList()
          })
        })
      }).catch(() => {
      })
    },resertCoin() {
      this.$confirm(`您确定对当前设备扣量吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var ids = this.dataListSelections.map(item => {
          return item.id
        })
        let postData = { ids: ids.join(',') }
        api.post('rest/vabc/task/resertCoin', postData).then(res => {
          this.$message.success('扣量成功')
          this.getDataList()
        })
      })
    },
    search() {
      this.tableForm.pageIndex = 1
      this.getDataList()
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.dataListLoading = true
      let postData = {
        'order': 'desc',
        'column': 'id'
      }
      Object.assign(postData, this.queryForm, this.tableForm)
      //postData.name=api.getLikeValue( postData.name);
      postData.oaid = api.getLikeValue(postData.oaid)
      postData.imei = api.getLikeValue(postData.imei)
      postData.imsi = api.getLikeValue(postData.imsi)
      postData.androidid = api.getLikeValue(postData.androidid)
      postData.username = api.getLikeValue(postData.username)
      postData.phonebrand = api.getLikeValue(postData.phonebrand)
      postData.phonemodel = api.getLikeValue(postData.phonemodel)
      postData.phonesystem = api.getLikeValue(postData.phonesystem)

      api.post('rest/vabc/device/list', postData).then(res => {
        this.dataList = res.datalist
        this.tableForm.total = res.total
      }).finally(() => {
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    onchangeTime(e) {
      if (e) {
        this.queryForm.createtime_begin = e[0]
        this.queryForm.createtime_end = e[1]
      } else {
        this.queryForm.createtime_begin = ''
        this.queryForm.createtime_end = ''
      }

    }
  }
}
</script>
